package com.github.baichuan.web.valication;


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证结果
 */
public class ValidationResult {
    /**
     * 验证结果是否包含错误
     */
    private boolean hasErrors;

    private Map<String, String> errorMessages;

    /**
     * 验证对象
     */
    private Object target;

    public ValidationResult(Object target){
        this.target = target;
        errorMessages = new HashMap<>();
    }

    public boolean hasErrors(){
        return hasErrors;
    }

    public Map<String, String> getErrorMessages(){
        return Collections.unmodifiableMap(errorMessages);
    }

    public Object getTarget(){
        return this.target;
    }

    public void error(String property, String message){
        hasErrors = true;

        if(errorMessages.containsKey(property)) {
            errorMessages.put(property, errorMessages.get(property) + "|" + message);
        } else {
            errorMessages.put(property, message);
        }
    }
}
